﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Net;
using System.IO;

namespace 更换IE代理IP
{
    class Program
    {
        [DllImport(@"wininet",
        SetLastError = true,
        CharSet = CharSet.Auto,
        EntryPoint = "InternetSetOption",
        CallingConvention = CallingConvention.StdCall)]

        public static extern bool InternetSetOption
        (
        int hInternet,
        int dmOption,
        IntPtr lpBuffer,
        int dwBufferLength
        );


        public static void SetProxy(string proxy)
        {
            //打开注册表
            RegistryKey regKey = Registry.CurrentUser;
            string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
            RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);
            //更改健值，设置代理，
            optionKey.SetValue("ProxyEnable", 1);
            optionKey.SetValue("ProxyServer", proxy);

            //激活代理设置【用于即使IE没有关闭也能更新当前打开的IE中的代理设置。】
            InternetSetOption(0, 39, IntPtr.Zero, 0);
            InternetSetOption(0, 37, IntPtr.Zero, 0);
        }


        static void Main(string[] args)
        {
            string[] proxList = {"165.228.128.10：3128", "59.10.72.198：8080", "125.248.206.194：8080", "125.243.249.194：8080", "210.107.249.50：3128", "210.107.249.50：3124", "210.180.39.92：8080", "210.102.99.71：38466", "58.80.207.41：3128", "133.1.16.172：3127", "150.65.32.66：3124", "203.178.133.2：3128", "203.178.133.10：3128", "203.178.133.2：3124", "203.178.133.3：3127", "203.178.133.10：3124" };
            //本事例中未对代理服务器设置密码的情况进行尝试
            foreach (string ip in proxList)
            {
                SetProxy(ip);
                if (prcessBaidu())
                {
                    Console.WriteLine(ip + "_____________TestOK");
                    break;
                }
                else
                {
                    Console.WriteLine(ip + "__false");
                }
            }
        }


        //成功返回true，错误返回false 
        public static Boolean prcessBaidu()
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
            myRequest.Method = "POST";
            //采用post方式提交访问163主页
            // Get response     
            try//当无法访问163网站时，下面的对象会有错误产生，所以用try..catch处理掉这些异常
            {
                Stream newStream = myRequest.GetRequestStream();//获取请求流
                // Send the data.        
                newStream.Close();//关闭请求流
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//获取应答对象
                StreamReader reader = new StreamReader(myResponse.GetResponseStream());//获取应答流
                string content = reader.ReadToEnd();//将流对象读取到string 中
                return true;
            }
            catch (Exception ex)
            {

            }
            return false;

        }
    }

}
